package com.zhongxiangspace.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhongxiangspace.entity.County;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.entity.StationHouse;
import com.zhongxiangspace.entity.VO.QueryAnalyzerOffice;
import com.zhongxiangspace.enums.*;
import com.zhongxiangspace.result.PageResult;
import com.zhongxiangspace.service.*;
import com.zhongxiangspace.tools.AnalyzerConditionTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工位
 */
@Controller
public class StationListController {

    @Autowired
    private CountyService countyService;
    @Autowired
    private OfficeBuildingService officeBuildingService;
    @Autowired
    private AdvertService advertService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private StationBranchService stationBranchService;
    @Autowired
    private ArticleService articleService;

    @GetMapping("lists/h2{value}")
    public String stationList(@PathVariable("value") String value, Model model) {
        //return "redirect:/lists/h2" + value + "/pv1";
        return  stationList(value,"pv1",model);
    }


    //工位
    @GetMapping("lists/h2{value}/{page}")
    public String stationList(@PathVariable("value") String value, @PathVariable(value = "page") String page, Model model) {
        long i = System.currentTimeMillis();
        Map<String, Object> map = new HashMap<>();
        if (page == "" || page.length() == 0) {
            page = "pv1";
        }

        Integer pageSize = PagesEnum.getEnumObject(page).getIndex();
        map.put("url", value);
        String regions = AnalyzerConditionTools.getCondition(value, "r");//缓存区域id
        map.put("regions", regions);
        String area = AnalyzerConditionTools.getCondition(value, "a");//缓存面积id
        String employees = AnalyzerConditionTools.getCondition(value, "e");//缓存人数
        if (!area.isEmpty()) {
            map.put("area", area);
        } else {
            map.put("area", employees);
        }
        String price = AnalyzerConditionTools.getCondition(value, "p");//缓存金额
        map.put("price", price);
        String decorate = AnalyzerConditionTools.getCondition(value, "d");//缓存装修
        map.put("decorate", decorate);
        QueryAnalyzer tradeAreaQueryAnalyzer = new QueryAnalyzer();
        tradeAreaQueryAnalyzer.setShowCode(1);
        tradeAreaQueryAnalyzer.setHotCode(1);
        if (!regions.isEmpty()) {
            tradeAreaQueryAnalyzer.setType(regions.substring(1, regions.length()));
            List<Map<String, Object>> tradeAreaVOS = this.countyService.queryTradeAreaByKeyword(tradeAreaQueryAnalyzer);
            map.put("tradeArea", tradeAreaVOS);
        }
        String tradeArea_id = AnalyzerConditionTools.getCondition(value, "t");//缓存商圈
        map.put("tradeArea_id", tradeArea_id);
        //区域
        QueryAnalyzer countQueryAnalyzer = new QueryAnalyzer();
        countQueryAnalyzer.setShowCode(1);
        countQueryAnalyzer.setType("0");
        List<County> countyList = this.countyService.queryByKeyword(countQueryAnalyzer);
        map.put("county", countyList);
        /*******************查询条件**************************************************/
        QueryAnalyzerOffice queryAnalyzerOffice = new QueryAnalyzerOffice();
        queryAnalyzerOffice.setPage(pageSize);
        //seo拼接
        String seoAttributeJoint="";
        //区域
        if (!regions.isEmpty()) {
            queryAnalyzerOffice.setCounty_id(Integer.valueOf(regions.substring(1, regions.length())));
            County county = this.countyService.queryOne(Integer.valueOf(regions.substring(1, regions.length())));
            seoAttributeJoint=seoAttributeJoint+county.getName();
        }
        //商圈
        if (!tradeArea_id.isEmpty()) {
            queryAnalyzerOffice.setTradeArea_id(Integer.valueOf(tradeArea_id.substring(1, tradeArea_id.length())));
            County county = this.countyService.queryOne(Integer.valueOf(tradeArea_id.substring(1, tradeArea_id.length())));
            seoAttributeJoint=seoAttributeJoint+county.getName();
        }
        //单价
        if (!price.isEmpty()) {
            queryAnalyzerOffice.setMinPrice(PriceExtenEnum.getEnumObject(price).getMin()*10);
            queryAnalyzerOffice.setMaxPrice(PriceExtenEnum.getEnumObject(price).getMax()*10);
            String priceStr=PriceExtenEnum.getEnumObject(price).getMin()*10 +"-"+PriceExtenEnum.getEnumObject(price).getMax()*10+"元/月";
            if(PriceExtenEnum.getEnumObject(price).getMax()*10>2001){
                 priceStr=PriceExtenEnum.getEnumObject(price).getMin()*10 +"以上";
            }
            seoAttributeJoint=seoAttributeJoint+priceStr;
        }

        /*****************************主楼盘数据**********************************/
        PageHelper.startPage(queryAnalyzerOffice.getPage(), 16,"station_branch.hotCode DESC");
        Page<Map<String, Object>> objectList = (Page<Map<String, Object>>) this.stationBranchService.queryList(queryAnalyzerOffice);

        for (Map<String, Object> objectMap : objectList ){
            List<StationHouse> hsmap=this.stationBranchService.queryStationHouse((Integer)objectMap.get("station_id"));
            objectMap.put("stationHouse",hsmap);
        }
        long total = objectList.getTotal();
        model.addAttribute("pageNum", objectList.getPageNum());
        model.addAttribute("totall", total);
        PageResult pageResult =new PageResult(objectList.getPageNum(),objectList.getPageSize(),objectList.getTotal(),objectList.getPages(), objectList.getResult(),true);
        map.put("pageResult",pageResult);
        /************************广告位*********************************************/
        QueryAnalyzer advertQueryAnalyzer = new QueryAnalyzer();
        advertQueryAnalyzer.setShowCode(1);
        List<Map<String, Object>> advertList = this.advertService.queryAdvert(advertQueryAnalyzer);
        map.put("advertList", advertList);
        /************************热门楼盘*********************************************/
        QueryAnalyzerOffice buildingQueryAnalyzerHot = new QueryAnalyzerOffice();
        buildingQueryAnalyzerHot.setHotCode(1);
        List<Map<String, Object>> queryListHot =  this.stationBranchService.queryList(buildingQueryAnalyzerHot);
        map.put("queryListHot", queryListHot);
        /*******************记录结果******************************************/
        /*******seo属性**********/
        model.addAttribute("seoAttributeJoint",seoAttributeJoint);
        model.addAttribute("data", map);
        return "list/station_list";
    }
}
